<?php

namespace addons\comments\controller;

use addons\comments\model\CommentsPostModel;
use addons\comments\validate\CommentsPostValidate;
use app\admin\model\user\User;
use think\addons\Controller;
use think\Request;

class Post extends Controller
{

    /**
     * @Notes:评论提交
     * @User: chenping
     * @email: 375129701@qq.com
     * @param Request $request
     */
    public function add(Request $request)
    {
        if($request->isPost())
        {
           $input = $request->post();

           $validate = new CommentsPostValidate();

           if(!$validate->check($input)) $this->error($validate->getError());

           $model = new CommentsPostModel();

           $result = $model->create($input);

           if($result)
           {
                $user = User::where("id",$input['from_uid'])->find();

                if(isset($user['avatar']))
                {
                    $user['avatar'] = _get_image_preview_url($user['avatar']);
                }

                $result['user']= $user;

               $this->success("评论成功",$request->url(),$result);
           }else{
               $this->error("系统错误请稍后再试");
           }
        }
    }


    public function commnents_page(Request $request)
    {
        $topic_id = $request->param("topic_id",0,"intval");
        $topic_type = $request->param("topic_type",0,"intval");
        $limit = $request->param("limit",15,"intval");
        if($topic_id)
        {
            $commentList =  CommentsPostModel::where("topic_type",$topic_type)
                ->where("topic_id",$topic_id)
                ->alias("a")
                ->leftJoin("cp_admin b","b.id=a.from_uid")
                ->leftJoin("cp_admin c","c.id=a.to_uid")
                ->where("a.is_status",1)
                ->where("a.topic_type",$topic_type)
                ->field("a.from_uid,a.id,a.content,a.create_time,a.update_time")
                ->field("b.nickname from_nickname,b.id from_uid,b.username from_username,b.avatar from_avatar")
                ->field("c.nickname touser_nickname,c.id touser_uid,c.username touser_username,c.avatar touser_avatar")
                ->paginate($limit);
            return json([
                "commentList"=>$commentList,
                "code" => 1,
                "file_domain" => _get_file_domain()
            ]);
        }else{
            $this->error("缺少主题id");
        }
    }




}